/*----------------------------------------------------------------------------
* This unit test tests the function gainCard(). 
*----------------------------------------------------------------------------*/

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int testGainCard(int supplyPos, struct gameState *G, int toFlag, int p) {

	int check;
	struct gameState preG;
	memcpy(&preG, G, sizeof(struct gameState));
	
	check = gainCard(supplyPos, G, toFlag, p);
	
	//check if supply pile is empty (0) or card is not used in game (-1)	
	if (supplyCount(supplyPos, &preG) < 1)
	{
		return -1;
	}

	//added card for [whoseTurn] current player:
	// toFlag = 0 : add to discard
	// toFlag = 1 : add to deck
	// toFlag = 2 : add to hand
  
	if (toFlag == 1)
	{
		preG.deck[p][preG.deckCount[p]] = supplyPos;
		preG.deckCount[p]++;
	}
	else if (toFlag == 2)
	{
		preG.hand[p][preG.handCount[p]] = supplyPos;
		preG.handCount[p]++;
	}
	else
	{
		preG.discard[p][preG.discardCount[p]] = supplyPos;
		preG.discardCount[p]++;
	}

	//decrease number in supply pile
	preG.supplyCount[supplyPos]--;

	assert(check == 0);

	assert(memcmp(&preG, G, sizeof(struct gameState)) == 0);

	return 0;

}

int main() {

	int i, j, p, supplyPos, toFlag, numTests = 50;
	struct gameState G;	
	SelectStream(2);
	PutSeed((long)1);	

    printf("TESTING gainCard():\n");

	for(i = 0; i < numTests; i++) {

		//reset game state with random bytes
		for (j = 0; j < sizeof(struct gameState); j++) {
			((char*)&G)[j] = floor(Random() * 256);
		}

		
		//randomly assign values
		supplyPos = floor(Random() * (treasure_map + 1));
		toFlag = floor(Random() * 3);
		p = floor(Random() * 4);
		G.whoseTurn = p;
		G.deckCount[p] = floor(Random() * MAX_DECK);
		G.handCount[p] = floor(Random() * MAX_HAND);
		G.discardCount[p] = floor(Random() * MAX_DECK);	

		testGainCard(supplyPos, &G, toFlag, p);
	}	

	printf("gainCard(): PASS. \n");
	
	return 0;
}